package com.designpattern.softwaredesignpatternstudy.pattern.singleton;

/**
 * @Author: Jiangxx
 * @Date: 2021/09/28
 * @Description: 单例模式：
 */
public class SingletonPattern {
}

// 懒汉式 由于懒汉式存在线程安全问题，故需要添加volatile,synchronized关键字
class LazySingleton {
    public static volatile LazySingleton instance = null;

    private LazySingleton(){}

    public static synchronized LazySingleton getLazySingletonInstance() {
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}

// 饿汉式
class HungrySingleton {
    public static final HungrySingleton instance = new HungrySingleton();

    private HungrySingleton() {}

    public static HungrySingleton getHungrySingletonInstance() {
        return instance;
    }
}
